import java.net.ServerSocket;
import java.net.Socket;
import java.io.*;
import java.net.*;
import java.lang.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.*;
import java.text.SimpleDateFormat;
import java.util.Calendar;

public class writeToFileClassHens {
 
  int client_id;
    Socket socket;    
    String folder;
    Long vehi;
    FileOutputStream fout;
    String tempTxt=null;
    StringBuilder line=new StringBuilder();
    //GeoCode geocode = new GeoCode();
    GeoFun geofun = new GeoFun();
    sendAlert sa = new sendAlert();
    
  
  public void makeFolder(String strFilename,String dataTxt,Long vehino) throws Exception
  {
   File fos=new File(strFilename);
   FileWriter fw=new FileWriter(fos,true);
   BufferedWriter bw=new BufferedWriter(fw);
   bw.write(dataTxt);
   bw.close();
   //geocode.callFunc(vehino,dataTxt);
   //geocode.sendSMSAlert(vehino,dataTxt);
  }
   public boolean readFile(String path,String Text,Long vehi1) 
   {
 String str1=null;
 try
 {
  FileInputStream fstream = new FileInputStream(path);
  DataInputStream in = new DataInputStream(fstream);
     BufferedReader br = new BufferedReader(new InputStreamReader(in)); 
   str1= br.readLine();
  in.close();
 }
 catch (Exception e){
   //System.err.println("Error: " + e.getMessage());
   str1="";
 }
  if(str1.indexOf(Text)==-1)
  {
   return true;
  }
  else
  {
   return false;
  }
    }
    
   public void readData(String strText) throws Exception
   {
    String[] newDateTime;
    String fTxt = null;
    String subFolder=null;
    String strLine=null;
    int gpsAV, latNS, lngEW;
    try
    {
       //System.out.println("Data string "+strText);
      
       strText = new StringBuffer(strText).insert(13, ",").toString();
       strText = new StringBuffer(strText).insert(33, ",").toString();
       
       String strText1 = strText.replace("(","");
       String strText2 = strText1.replace(")","");
       String[] data3=strText2.split(",");
       
       vehi = Long.parseLong(data3[0]);
       
       if(data3[2].indexOf("A") > -1)
         gpsAV = data3[2].indexOf("A");
       else 
         gpsAV = data3[2].indexOf("V");      
       
       
       String date1 = data3[2].substring(0,gpsAV);
       
       String[] date = date1.split("(?<=\\G.{2})");
       
       
       if(data3[2].indexOf("N") > -1)
         latNS = data3[2].indexOf("N");
       else 
         latNS = data3[2].indexOf("S");
       
       
       if(data3[2].indexOf("E") > -1)
         lngEW = data3[2].indexOf("E");
       else 
         lngEW = data3[2].indexOf("W");
       
       String latTxt = data3[2].substring((gpsAV+1),(latNS+1));       
       latTxt = latTxt.replace(".","");
       Double latTxt1 = geofun.round(geofun.convertLat(geofun.calLat(latTxt)),8);
       String newLat = Double.toString(latTxt1).replace(".","");     
      
       
       String lngTxt = data3[2].substring((latNS+1),(lngEW+1));
       lngTxt = lngTxt.replace(".","");
       Double lngTxt1 = geofun.round(geofun.convertLong(geofun.calLong(lngTxt)),8);
       String newLng = Double.toString(lngTxt1).replace(".","");
       
       
       
       String remain = data3[2].substring((lngEW+1));
       //String speed = remain.substring(0,5);
       long speed1 = Math.round(Double.parseDouble(remain.substring(0,5))* 1.609344);
       
       String time1 = remain.substring(5,11);
       String[] time = time1.split("(?<=\\G.{2})");
       
       newDateTime=geofun.makeDateTime(date[2],date[1],date[0],time[0],time[1],time[2]).split(",");  
       
       String orient = remain.substring(11,17);
       
       String IOState1 = remain.substring(17,25);
       
       String[] IOState = IOState1.split("(?<=\\G.{1})");
       
       String otherParam = "";
       for(int i=0; i < 8; i++)
       {
         otherParam += "["+(i+1)+"="+IOState[i]+"] ";
       }
       
       String mileStr = remain.substring(26,remain.length()-1).toString();       
       otherParam += "[Mileage="+Long.parseLong(mileStr,16)+"] ";
       
       fTxt="@"+vehi+","+newLng+","+newLat+","+speed1+",0,0,0,"+otherParam+","+newDateTime[0]+","+newDateTime[1]+"#";  
       
       //System.out.println("vehi "+fTxt);
     
      //subFolder="E:/xampp/htdocs/src/data/"+newDateTime[0]+"/";
      subFolder="/opt/lampp/htdocs/src/data/"+newDateTime[0]+"/";
      
      //System.out.println("subFolder "+subFolder);
      File sub = new File(subFolder);
      try
      {
        if(sub.mkdirs()){}
        else{}
      }
      catch(Exception e)
      {
        e.printStackTrace();
      }
      subFolder+=vehi+".txt";
      //System.out.println(subFolder);      
      
      if(readFile(subFolder,fTxt,vehi))
      {
        //System.out.println("Data "+fTxt);
        makeFolder(subFolder,fTxt,vehi);
        sa.sendSMSAlert(fTxt);
      }
    
    }   
    catch(Exception e)
    {
      //e.printStackTrace();
       //System.out.println("Problem finding file");
    }
  }
 

 /*public static void main (String[] args) 
 {
   writeToFileClassHens wtf= new writeToFileClassHens();
   try
   {
    //wtf.readData("$CHD5991,27,07,09,10,10,01,17266264N,078260380E,00.0,164#$CHD5991,27,07,09,10,10,21,17266264N,078260380E,00.0,178#");
    //wtf.readData("(08128062149,BP0500008128062149,111104A2302.2291N07230.7121E000.0131224338.5500000000L00123AF1)");
     wtf.readData("(033032029683BP05000033032029683111115A2841.9259N07706.8751E000.0052842221.7200000000L00000005) ");
   }
   catch(Exception e)
   {
    
   }
 }*/
    
}
